<script type="text/javascript">
	
	function findTotalDevicesRecords()
	{
		<?php foreach($devices as $e => $device) { ?>
				var total = 0;
				<?php foreach($branches as $b => $branch) { ?>
					if($('#NhdDeviceResourceValue_<?php echo $branch["Branch"]["id"]."_".$device["Device"]["id"] ?>').val().replace(",", "") != '')
						total += parseInt($('#NhdDeviceResourceValue_<?php echo $branch["Branch"]["id"]."_".$device["Device"]["id"] ?>').val().replace(",", ""));
				<?php } ?>
				$('#devices_total_<?php echo $device["Device"]["id"] ?>').html($.formatNumber(total, {format:"#,###"}));
		<?php } ?>
	}

	jQuery(document).ready(function($) {
		findTotalDevicesRecords();

		
		$("#devices_record_frame input").blur(function(){
			findTotalDevicesRecords();
		});		
	});

</script>

<div class="NhdDeviceResources form" id="devices_record_frame">
	<?php echo $this->render('_filter_add', '');?>
	
<?php echo $this->Form->create('NhdDeviceResource');?>
	<fieldset>
		<legend><?php __('Thống kê trang thiết bị'); ?></legend>
	<?php
		echo $this->Form->input('year', array('type'=>'hidden', 'value'=>$year));
		echo $this->Form->input('quarter', array('type'=>'hidden', 'value'=>$quarter));
		echo $this->Form->input('city_id', array('type'=>'hidden', 'value'=>$city["id"]));
		echo $this->Form->input('unit_id', array('label'=>'Đơn vị', 'empty'=> true));
	?>	
	
	<div class="input text required">
		<label for="NhdDeviceResourceValue">Bảng cập nhật</label>		
		<table cellpadding="0" cellpadding="0" id="input_table">
			<tr>
				<th></th>
				<?php foreach($devices as $key => $device) { ?>
					<th><?php echo $device["Device"]["name"] ?></th>
				<?php } ?>
			</tr>
			<?php foreach($branches as $b => $branch) { ?>
				<tr>
					<td><?php echo $branch["Branch"]["name"] ?></td>
					<?php foreach($devices as $e => $device) { ?>
						<td>
							<input class="iit" type="text" id="NhdDeviceResourceValue_<?php echo $branch["Branch"]["id"]."_".$device["Device"]["id"] ?>" value="<?php if($values[$branch["Branch"]["id"]][$device["Device"]["id"]] != "") echo number_format($values[$branch["Branch"]["id"]][$device["Device"]["id"]],0,".", ",") ?>" maxlength="255" name="data[values][<?php echo $branch["Branch"]["id"] ?>][<?php echo $device["Device"]["id"] ?>]" />
						</td>
					<?php } ?>
				</tr>
			<?php } ?>
			<tr>
				<td>Tổng cộng</td>
				<?php foreach($devices as $e => $device) { ?>
					<td style="text-align: right" id="devices_total_<?php echo $device["Device"]["id"] ?>">
					</td>
				<?php } ?>
			</tr>
		</table>
	</div>
	</fieldset>
<?php echo $this->Form->end(__('Lưu thông tin', true));?>
</div>



<div class="actions">
	<h3><?php __('Chức năng'); ?></h3>
	<ul>
		<li><?php echo $this->Html->link(__('Danh sách đơn vị RHM', true), array('controller' => 'branches', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('Thêm đơn vị RHM', true), array('controller' => 'branches', 'action' => 'add')); ?> </li>		
	</ul>
</div>